﻿singleApp.controller('usersCtrl', ['$http', '$scope', 'PageService', function ($http, $scope, PageService) {
    $scope.list = [];
    $scope.Page = {
        pageindex: 1,
        pagesize: "10",
        user_name: "",
        phone: "",
        parent_user_name: "",
        TotalPages: 0,
        TotalRecords: 0,
        PageWindowSize: 10,
        PageRange: [],
    };

    //Init
    $scope.init = function () {
        layer.load(2);

        var parms = [];
        parms.push("pageindex=" + $scope.Page.pageindex);
        parms.push("pagesize=" + $scope.Page.pagesize);
        parms.push("user_name=" + $scope.Page.user_name);
        parms.push("phone=" + $scope.Page.phone);
        parms.push("parent_user_name=" + $scope.Page.parent_user_name);
        var api_url = window.singleConfig.rootUrl + "api/admin/UsersService/pageList?" + parms.join("&");

        $http.get(api_url).then(function (result) {
            var data = result.data;

            $scope.list = data.records;
            $scope.Page.TotalPages = data.totalPages;
            $scope.Page.TotalRecords = data.count;
            $scope.Page.PageRange = PageService.getPageRange($scope.Page.pageindex, $scope.Page.PageWindowSize, $scope.Page.TotalPages);
            layer.closeAll();
        }, function () { });
    };

    $scope.init();


    $scope.GoToPage = function (page) {
        if (page == $scope.Page.pageindex)
            return;
        $scope.Page.pageindex = page;
        $scope.init();
    };

    $scope.search = function () {
        $scope.init();
    };

    $scope.showQRCode = function (item) {
        $('#divQRCode').html("").qrcode({ width: 164, height: 164, text: $('#divQRCode').attr("data-url") + item.token });
        layer.open({
            type: 1,
            title: "查看二维码 - " + item.user_name,
            area: ['400px', '290px'], //宽高
            content: $("#divQRCodePopup")
        });
    };

    $scope.changePassword = function (item) {
        x_admin_show('修改密码', 'resetmemberpwd?id=' + item.id, 600, 400);
    };

    $scope.memberEdit = function (item) {
        x_admin_show('修改用户信息', 'member_edit?id=' + item.id, 600, 470);
    };

    $scope.changeParent = function (item) {
        x_admin_show('修改用户信息', 'change_parent?id=' + item.id, 600, 470);
    };

    $scope.viewChildren = function (item) {
        x_admin_show('查看下级', 'view_children?id=' + item.id, 700, 470);
    };

    $scope.deleteUser = function (item) {
        layer.confirm("确认禁用该用户吗？", {
            btn: ['确认', '取消']
        }, function () {
            var api_url = window.singleConfig.rootUrl + 'api/admin/UsersService/disable_user';
            layer.load(2);
            $http({
                method: 'POST',
                url: api_url,
                data: $.param({ id: item.id }),
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            })
            .then(function (result) {
                var data = result.data;
                if (data.code == "success") {
                    layer.closeAll();
                    layer.msg("更新成功！");
                }
                else {
                    layer.closeAll();
                    layer.msg(data.message);
                }
            }, function (data) {
                layer.msg("系统错误!");
            });
        }, function () {

        });
    };

    $scope.export_users = function () {
        window.open(window.singleConfig.rootUrl + "admin/export_users");
    };
}]);